'''
author:1900
@time:2022/06/01
'''

import os
import time
from tkinter import messagebox

import win32gui
import win32api
import win32con
from pykeyboard import PyKeyboard
from ctypes import *
import tkinter as tk



def LoginQQ(username, password):
    # 运行QQ
    os.system('"C:\Program Files (x86)\Tencent\QQ\Bin\QQScLauncher.exe"')
    time.sleep(2)
    # 获取QQ的窗口句柄
    # 参数1是类名,参数2是QQ软件的标题
    a = win32gui.FindWindow(None, "QQ")
    # 获取QQ登录窗口的位置
    loginid = win32gui.GetWindowPlacement(a)
    # print(loginid)

    # 定义一个键盘对象
    k = PyKeyboard()
    # 把鼠标放置到登陆框的输入处
    windll.user32.SetCursorPos(loginid[4][0] + 306, loginid[4][1] + 250)

    # 按下鼠标再释放
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  # press mouse
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)  # release mouse
    time.sleep(1)
    # 输入用户名
    k.type_string(username)
    time.sleep(0.2)
    # 按下tab，切换到输入密码的地方
    k.tap_key(k.tab_key)
    # 输入密码
    k.type_string(password)

    # 按下回车
    time.sleep(1)
    win32api.keybd_event(13, 0, 0, 0)
    win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)

class GUI:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('LoginQQ')
        self.root.geometry("400x200+550+250") # 长x宽+x*y
        self.interface()

    def interface(self):
        """"界面编写位置"""
        # 文本框
        self.Label0 = tk.Label(self.root, text="QQ账号:")
        self.Label0.place(x=80,y=30,width=50,height=30)
        self.Label1 = tk.Label(self.root, text="QQ密码:")
        self.Label1.place(x=80, y=90, width=50, height=30)
        # 输入框
        self.Entry0 = tk.Entry(self.root)
        self.Entry0.place(x=140, y=30, width=170, height=30)
        self.Entry1 = tk.Entry(self.root,show='*')
        self.Entry1.place(x=140, y=90, width=170, height=30)
        # 按钮
        self.Button0 = tk.Button(self.root, text="确定登录",command=self.btn0event)
        self.Button0.place(x=80, y=140, width=120, height=30)
        self.Button1 = tk.Button(self.root, text="退出", command=self.root.destroy)
        self.Button1.place(x=220, y=140, width=120, height=30)
        pass
    def btn0event(self):
        # btn0按钮事件
        info_username = self.Entry0.get()
        info_password = self.Entry1.get()
        if info_username == "" or info_password == "":
            messagebox.showerror("error", "请输入账号或密码！")
            self.Entry0.delete(0, tk.END)  # 清空输入框
            self.Entry1.delete(0, tk.END)
        else:
            print(info_username)
            print(info_password)
            LoginQQ(info_username,info_password)
            self.Entry0.delete(0,tk.END) # 清空输入框
            self.Entry1.delete(0,tk.END)

if __name__ == '__main__':
    a = GUI()
    a.root.mainloop()



